package com.pansoft.openplanet.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.utils.ResStringUtil;
import com.efounder.view.CircleImageView;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.TotalAssetBean;
import com.pansoft.openplanet.manager.CurrencyAndBourseManager;

import java.util.List;

public class TotalAssetsAdapter extends RecyclerView.Adapter {

    private static final int TYPE_HEADER = 0;

    private static final int TYPE_BODY = 1;
    private Context mContext;
    private List<TotalAssetBean> assetBeans;
    /**
     * 眼睛按钮的点击事件
     */
    private OnEyeShowNumClickListener onEyeShowNumClickListener;

    public TotalAssetsAdapter(Context mContext, List<TotalAssetBean> assetBeans) {
        this.mContext = mContext;
        this.assetBeans = assetBeans;
    }

    /**
     * 设置眼睛按钮的点击事件
     */
    public void setOnEyeShowNumClickListener(OnEyeShowNumClickListener onEyeShowNumClickListener) {
        this.onEyeShowNumClickListener = onEyeShowNumClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.open_planet_total_assets_adapter_header,
                    parent, false);
            return new HeaderViewHolder(view);
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.open_planet_total_assets_adapter_item,
                parent, false);
        return new BodyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        //
        if (holder.getItemViewType() == TYPE_HEADER) {
            TotalAssetBean assetBean = assetBeans.get(position);
            if (assetBean.isShowNum()) {
                ((HeaderViewHolder) holder).tvLeftValue.setText(assetBean.getRmbValue());
                //设置眼睛图标
                ((HeaderViewHolder) holder).ivShowNum.setImageResource(R.drawable.icon_show_password);
            } else {
                ((HeaderViewHolder) holder).tvLeftValue.setText("******");
                ((HeaderViewHolder) holder).ivShowNum.setImageResource(R.drawable.icon_hide_password);
            }
            //
            ((HeaderViewHolder) holder).tvTotalTitle.setText(String.format(ResStringUtil
                            .getString(R.string.open_planet_asset_total_assets),
                    CurrencyAndBourseManager.getCurrencyUnitCode()));
            //眼睛按钮的点击事件
            ((HeaderViewHolder) holder).ivShowNum.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onEyeShowNumClickListener.isShowNmu();
                }
            });
        } else {
            TotalAssetBean totalAssetBean = assetBeans.get(position);
            LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext,
                    ((BodyViewHolder) holder).ivImage, totalAssetBean.getAssetIcon(),
                    R.drawable.btc, R.drawable.btc, 100);
            ((BodyViewHolder) holder).tvName.setText(totalAssetBean.getAssetName());
            ((BodyViewHolder) holder).tvRmbRate.setText(totalAssetBean.getRate());
            //
            if (totalAssetBean.isShowNum()) {
                ((BodyViewHolder) holder).tvAssetValue.setText(totalAssetBean.getAssetValue());
                ((BodyViewHolder) holder).tvAssetRmbValue.setText(totalAssetBean.getRmbValue());
            } else {
                ((BodyViewHolder) holder).tvAssetValue.setText("****");
                ((BodyViewHolder) holder).tvAssetRmbValue.setText("****");
            }
        }
    }

    @Override
    public int getItemCount() {
        return assetBeans.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        } else {
            return TYPE_BODY;
        }
    }

    public class HeaderViewHolder extends RecyclerView.ViewHolder {
        ImageView ivBackground;//背景图
        TextView tvLeftValue;
        TextView tvRightValue;
        TextView tvTotalTitle;
        //眼睛图标
        ImageView ivShowNum;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            ivBackground = itemView.findViewById(R.id.iv_background);
            tvLeftValue = itemView.findViewById(R.id.tv_assets_left_value);
            tvTotalTitle = itemView.findViewById(R.id.tv_total);
            ivShowNum = itemView.findViewById(R.id.iv_show_num);
            // tvRightValue = itemView.findViewById(R.id.tv_assets_right_value);
        }
    }

    public class BodyViewHolder extends RecyclerView.ViewHolder {

        CircleImageView ivImage;
        TextView tvName;
        TextView tvRmbRate;
        TextView tvAssetValue;
        TextView tvAssetRmbValue;

        public BodyViewHolder(View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_image);
            tvName = itemView.findViewById(R.id.tv_name);
            tvRmbRate = itemView.findViewById(R.id.tv_rmb_rate);
            tvAssetValue = itemView.findViewById(R.id.tv_asset_value);
            tvAssetRmbValue = itemView.findViewById(R.id.tv_asset_rmb_value);
        }
    }

    /**
     * 点击眼睛按钮是否展示数量
     */
    public interface OnEyeShowNumClickListener {
        void isShowNmu();
    }
}
